//
//  STMemorailModel.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
class STMemorailViewModel{
    var dataSource = [STMemorailModel]()
    var lately = false
}
extension STMemorailViewModel{
    func getPageData(_ type:Int, onSuccess: ((_ models: [STMemorailModel]) ->Void)?){
        let dataHelper = STMemorailModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: STMemorailModel.self) as! [STMemorailModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        var sources = [STMemorailModel]()
        for model in sortedModels {
            let eventDay = model.date.substring(from:4)
            let next = Date().toString(format: "yyyy") + eventDay
            let (nextReminderDate, daysUntilReminder) = String.calculateNextReminderDate(from: next, withWay: model.way)
            if type != 0 {
                if daysUntilReminder < 31 {
                    sources.append(model)
                }
            }
        }
        let resultSource = type == 0 ? sortedModels : sources
        self.dataSource = resultSource
        onSuccess?(self.dataSource)
    }
}

@objcMembers
class STMemorailModel: STBaseModel,Codable {
    var name = ""
    var way = 0
    var date = ""
    var remind = 0
    var isDefault = true
    override class func getTableName() -> String {
        return "BreedingCostTab"
    }
}
